package com.lfy.kcat.user.config;

import com.lfy.kcat.user.template.BloomFilterTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author leifengyang
 * @version 1.0
 * @date 2025/9/16 17:02
 * @description:
 */
@Slf4j
@Configuration
public class AppConfig {
    @Value("${app.executor.corePoolSize}")
    private int corePoolSize;
    @Value("${app.executor.maxPoolSize}")
    private int maxPoolSize;
    @Value("${app.executor.keepAliveTime}")
    private int keepAliveTime;
    @Value("${app.executor.queueCapacity}")
    private int queueCapacity;

    @Bean("appThreadPoolExecutor")
    ThreadPoolExecutor threadPoolExecutor(){
        return new ThreadPoolExecutor(
            corePoolSize,
            maxPoolSize,
            keepAliveTime,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(queueCapacity),
           new ThreadPoolExecutor.CallerRunsPolicy());
    }


    /**
     * 做延迟任务的线程池
     * @return
     */
    @Bean("scheduledPoolExecutor")
    ScheduledThreadPoolExecutor scheduledPoolExecutor(){


        return  new ScheduledThreadPoolExecutor(4);
    }


    /**
     * 项目启动成功 ： ApplicationRunner 里面的方法就会运行一次
     * @return
     */

    @Bean
    ApplicationRunner applicationRunner(BloomFilterTemplate template){

        return ( args)-> {
            log.info("项目启动完成，准备初始化 bf ....");
            template.initDramasIdBloomFilter();
        };
    }
}
